package com.lu;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.pinyin.PinyinUtil;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program lu-scala01
 * @description:
 * @author: zhanglu
 * @create: 2022-03-05 21:51:00
 */
public class Test {

    @org.junit.Test
    public void test01(){
        List<Map<String, Object>> ls = new ArrayList<>();
        Map<String, Object> m1 = new HashMap<>();
        m1.put("id", 1);
        m1.put("name", "A物料");
        m1.put("num", 10);
        ls.add(m1);
        m1 = new HashMap<>();
        m1.put("id", 1);
        m1.put("name", "A物料");
        m1.put("num", 5);
        ls.add(m1);
        m1 = new HashMap<>();
        m1.put("id", 2);
        m1.put("name", "B物料");
        m1.put("num", 5);
        ls.add(m1);
        m1 = new HashMap<>();
        m1.put("id", 2);
        m1.put("name", "B物料");
        m1.put("num", 6);
        ls.add(m1);
        m1 = new HashMap<>();
        m1.put("id", 3);
        m1.put("name", "C物料");
        m1.put("num", 7);
        ls.add(m1);

        List<Map<String, Object>> res = new ArrayList<>();
        ls.stream().collect(Collectors.groupingBy(o -> o.get("id"))).entrySet().stream()
                .forEach(o -> {
                    Map<String, Object> m = new HashMap<>();
                    m.put("id", o.getKey());
                    m.put("name", Optional.of(o.getValue().get(0).get("id")).orElse(""));
                    m.put("num", o.getValue().stream().collect(Collectors.summingInt(e -> (Integer) e.getOrDefault("num", 0))));
                    res.add(m);
                });
        System.out.println(res);
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.between(DateUtil.parseDate("2021-08-20"), DateUtil.date(), DateUnit.DAY));
//
//        String s1 = PinyinUtil.getPinyin("拉拜师 sigo");
//        System.out.println(s1);
//        s1 = PinyinUtil.getFirstLetter("拉拜师 sigo", "");
//        System.out.println(s1);

        Worker worker = new Worker();
        System.out.println(worker.name);
        worker.print();

        System.out.println("==============");

        Person person = new Worker();
        System.out.println(person.name);    //静态绑定属性
        person.print(); //动态绑定方法

    }

    private static class Person{
        public String name = "person";
        private void print(){
            System.out.println("hello person");
        }
    }

    private static class Worker extends Person{
        public Worker(){
            this.name = "Worker";
        }
        private void print(){
            System.out.println("hello worker");
        }

    }

}
